package Entwickler;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;

public class Polygon {
	public ArrayList<Point> points = new ArrayList<Point>();
	public ArrayList<int[]> lines = new ArrayList<int[]>();
	public double xPos = 0.0;
	public double yPos = 0.0;
	public double zPos = 0.0;
	
	private double xPosRotated = xPos;
	private double yPosRotated = yPos;
	private double zPosRotated = zPos;
	
	public Polygon(double xPos, double yPos, double zPos){
		this.xPos = xPos;
		this.yPos = yPos;
		this.zPos = zPos;
	}
	
	public Graphics2D paint(Graphics2D bufferedGraphics2d) {
		bufferedGraphics2d.setColor(Color.black);
		for (int i = 0; i < lines.size(); i++) {
			Point p1 = new Point(0, 0, 0, this);
			Point p2 = new Point(0, 0, 0, this);
			for (int j = 0; j < points.size(); j++) {
				if (points.get(j).identifier == lines.get(i)[0]) {
					p1 = points.get(j);
				}
				if (points.get(j).identifier == lines.get(i)[1]) {
					p2 = points.get(j);
				}
			}
			bufferedGraphics2d.setColor(p2.color);
			bufferedGraphics2d.drawLine(400+(int)p1.getTransformedX(), 300-(int)p1.getTransformedY(), 
					400+(int)p2.getTransformedX(), 300-(int)p2.getTransformedY());
		}
		return bufferedGraphics2d;
	} 
	
	public void rotate(double x, double y, double z){
		xPosRotated = xPos;
		yPosRotated = yPos;
		zPosRotated = zPos;
		
		double xPosRotatedTmp = xPos;
		double yPosRotatedTmp = yPos;
		
		yPosRotatedTmp = yPosRotated*Math.cos(x) - zPosRotated*Math.sin(x);
		zPosRotated = yPosRotated*Math.sin(x) + zPosRotated*Math.cos(x);
		yPosRotated = yPosRotatedTmp;
	
		xPosRotatedTmp = xPosRotated*Math.cos(y) + zPosRotated*Math.sin(y);
		zPosRotated = -xPosRotated*Math.sin(y) + zPosRotated*Math.cos(y);
		xPosRotated = xPosRotatedTmp;
	
		xPosRotatedTmp = xPosRotated*Math.cos(z) - yPosRotated*Math.sin(z);
		yPosRotated = xPosRotated*Math.sin(z) + yPosRotated*Math.cos(z);
		xPosRotated = xPosRotatedTmp;
		
		xPos = xPosRotated;
		yPos = yPosRotated;
		zPos = zPosRotated;
		
		
		for (int i = 0; i < points.size(); i++) {
			points.get(i).rotate(x, y, z);
		}
	}
	
	public void scale(double x, double y, double z){
		xPos *= x;
		yPos *= y;
		zPos *= z;
		for (int i = 0; i < points.size(); i++) {
			points.get(i).scale(x, y, z);
		}
	}
	
}
